package com.nl.pulsardemo.pool.controller;

import com.nl.pulsardemo.pool.config.produce.MyProducerPoolConfig;
import com.nl.pulsardemo.pool.config.produce.PooledProducerFactory;
import com.nl.pulsardemo.pool.service.IProduce;
import com.nl.pulsardemo.pool.vo.ResultVO;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.pulsar.client.api.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author : ZhaoY
 * @description : 生产者
 * @date : 2021-02-01 15:34
 **/
@RestController
@RequestMapping("/produce")
public class ProduceController {
    @Autowired
    private IProduce iProduce;

    @Autowired
    private GenericObjectPool<Producer> producerPool;
//    GenericObjectPool<Producer> producerPool=new GenericObjectPool(new PooledProducerFactory(), new MyProducerPoolConfig());


    @PostMapping(value = "/sync")
    @CrossOrigin(origins = "*")
    public ResultVO SyncSend(@RequestBody String param) throws Exception {
        Producer producer = producerPool.borrowObject();
        ResultVO resultVO = iProduce.SyncSend(producer, param);
        producerPool.returnObject(producer);
        return resultVO;
    }

    @PostMapping("/async")
    @CrossOrigin(origins = "*")
    public ResultVO AsyncSend(String param) throws Exception {
        Producer producer = producerPool.borrowObject();
        ResultVO resultVO = iProduce.AsyncSend(producer, param);
        producerPool.returnObject(producer);
        return resultVO;
    }
}
